// pages/Personal_info/Personal_info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phonenumber:'未填写',
    ID:'获取中...',
    phone:'0',
    loading:false,
    saveAvailable:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'pwdWithSalt',
      fail(res){
        wx.navigateTo({
          url: '../../pages/landing/landing',
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            wx.showToast({
              title: '调用登陆失败',
              icon: 'loading',
              duration: 1000
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: 'pwdWithSalt',
      success (res) {
        wx.request({
          url: 'https://localhost:8080/getIDandPhone', 
          data: {
            pwdWithSalt:res.data
          },
          success (res) {
            //不在线的情况
            if(res.data=='2')
            {
              wx.navigateTo({
                url: '../../pages/landing/landing',
                success: function (res) {
                  console.log(res)
                },
                fail: function (res) {
                  wx.showToast({
                    title: '调用登陆失败',
                    icon: 'loading',
                    duration: 1000
                  })
                }
              })
            }
            else if(res.data.phone=='null')
            {
              that.setData({
                ID:res.data.openID,
                phonenumber:'未填写'
              })
            }
            else
            {
              that.setData({
                ID:res.data.openID,
                phonenumber:res.data.phone
              })
            }

          },
        })
      }
    })
  },

  /**
   * 监听手机号输入
   */
  listenerPhoneInput: function(e) {
    this.data.phone = e.detail.value;

},

available:function(){
  this.setData({
    saveAvailable:true
  })
},

Bcopy:function(){
  var that =this
  wx.setClipboardData({
    data: that.data.ID,
    success: function (res) {
      wx.getClipboardData({
        success: function (res) {
          wx.showToast({
            title: '复制成功'
          })
        }
      })
    }
  })
},

  Bsave: function() {
    //保存
    this.setData({
      loading:true
    })
    console.log(this.data.phone)
    if(this.data.phone=='0')
    {
      wx.showToast({
        title: '输入不能为空',
        image: 'err.png',
        duration: 700
      })
      this.setData({
        loading:false
      })
    }
    else
    {
      var phone=this.data.phone
      if(!(/^1[3456789]\d{9}$/.test(phone)))
      { 
        wx.showToast({
          title: '请输入手机号',
          image: 'err.png',
          duration: 700
        })
        this.setData({
          loading:false
        })
      }
      else
      {
        var that = this
        wx.getStorage({
          key: 'pwdWithSalt',
          success (res) {
            wx.request({
              url: 'https://localhost:8080/setPhone', //仅为示例，并非真实的接口地址
              data: {
                pwdWithSalt:res.data,
                phone:that.data.phone
              },
              success (res) {
                if(res.data=='1')
                {
                  that.setData({
                  phonenumber:that.data.phone,
                  loading:false
                  })
                  wx.showToast({
                    title: '修改成功',
                    duration: 700
                  })
                  that.setData({
                    saveAvailable:false
                  })
                }
              }
            })
          }
        })
      }
    }
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})